package com.udaos.modules.site.controllers;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.adaptor.WhaleAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.site.Ud_site_information;
import com.udaos.modules.site.services.UdSiteInfomationService;


/**
 * 资讯WEB层
 * @author mutaotao
 * @time 2016-11-07 17:19:18
 * 
 */
@IocBean
@At("/platform/site/infomation")
@Filters({ @By(type = PrivateFilter.class)})
public class UdSiteInfomationController {
	
	@Inject
	private UdSiteInfomationService siteInfomationService;


	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/site/infomation/index.html")
	@RequiresAuthentication
	public void index() {

	}
	/**
	 * 加载数据
	 * @param name   名称
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", false);
		cnd.desc("createAt");
		if (!Strings.isBlank(name))
			cnd.and("title", "like", "%" + name + "%");
		return siteInfomationService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 新增
	 * @param unitid
	 * @return
	 */
	@At
	@Ok("beetl:/platform/site/infomation/add.html")
	@RequiresAuthentication
	public void add() {
		
	}
	/**
	 * 新增
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object addDo(@Param("..") Ud_site_information information, @Param("createAtStr") String createAtStr,HttpServletRequest req) {
		try {
			//设置创建时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			int createAt = (int) (sdf.parse(createAtStr).getTime() / 1000);
			information.setCreateAt(createAt);
			//设置创建人
			siteInfomationService.insert(information);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/site/infomation/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		return siteInfomationService.fetch(id);
	}
	
	/**
	 * 修改
	 * @param member
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object editDo(@Param("..") Ud_site_information information, @Param("createAtStr") String createAtStr, HttpServletRequest req) {
		try {
			//设置修改
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			int createAt = (int) (sdf.parse(createAtStr).getTime() / 1000);
			information.setCreateAt(createAt);
			siteInfomationService.updateIgnoreNull(information);
	   		return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_site_information information = siteInfomationService.fetch(id);
        	information.setDelFlag(true);
        	siteInfomationService.updateIgnoreNull(information);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}